<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">生产商配置</p>
    </div>
    <div class="view-container">
      <el-row :gutter="10" class="mb8" v-if="list.length == 0">
        <el-col :span="1.5">
          <el-button
            class="btn-success"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新建</el-button>
        </el-col>
      </el-row>
      <el-table :data="list" border>
        <el-table-column label="公司名称" prop="corporateName" align="center" width="220" />
        <el-table-column label="产地" prop="placeOfOrigin" align="center" :show-overflow-tooltip="true" />
        <el-table-column label="公司地址" prop="corporateAddress" align="center" :show-overflow-tooltip="true" width="220" />
        <el-table-column label="防伪次数" prop="antiCounterfeitingFrequency" align="center" :show-overflow-tooltip="true" />
        <!-- <el-table-column label="防伪内容" prop="content" align="center" :show-overflow-tooltip="true" /> -->
        <!-- <el-table-column label="图片" align="center" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <img v-if="scope.row.officialQrCodeUrl" :src="scope.row.officialQrCodeUrl" width="50" height="50" />
          </template>
        </el-table-column> -->
        <el-table-column label="备注" prop="remark" align="center" :show-overflow-tooltip="true" />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope" v-if="scope.row.roleId !== 1">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加或编辑角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" top="50px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="公司名称" prop="corporateName">
          <el-input v-model="form.corporateName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="产地" prop="placeOfOrigin">
          <el-input v-model="form.placeOfOrigin" placeholder="请输入产地" />
        </el-form-item>
        <el-form-item label="公司地址" prop="corporateAddress">
          <el-input v-model="form.corporateAddress" placeholder="请输入公司地址" />
        </el-form-item>
        <el-form-item label="防伪次数" prop="antiCounterfeitingFrequency">
          <el-input v-model.number="form.antiCounterfeitingFrequency" placeholder="请输入防伪次数" />
        </el-form-item>
        <!-- <el-form-item label="防伪内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入防伪内容" />
        </el-form-item>
        <el-form-item label="上传图片:" prop="officialQrCodeUrl">
          <el-upload
            class="avatar-uploader"
            action="#"
            :auto-upload="false"
            :on-change="onUploadType"
            accept=".jpg,.jpeg,.png,.gif,.bmp,.JPG,.JPEG,.PNG,.GIF,.BMP"
            :show-file-list="false"
          >
            <img v-if="form.officialQrCodeUrl" :src="form.officialQrCodeUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item> -->
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="btn-success mr20 width100" @click="submitForm">确 定</el-button>
        <el-button class="width100" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { infoConfigList, infoConfigAdd, infoConfigDetail } from "@/api/system/setting/infoConfig/infoConfig"
import { uploadImg } from "@/api/system/upload/upload"

export default {
  name: "list",
  data() {
    return {
      list: [],
      // 弹出层标题
      title: "",
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        corporateName: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
        placeOfOrigin: [
          { required: true, message: "产地不能为空", trigger: "blur" }
        ],
        corporateAddress: [
          { required: true, message: "公司地址不能为空", trigger: "blur" }
        ],
        antiCounterfeitingFrequency: [
          { required: true, message: "防伪次数不能为空", trigger: "blur" },
          { type: 'number', message: '防伪次数必须为数字'}
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取角色列表
    getList() {
      infoConfigList().then(res => {
          this.list = res.rows
        }
      )
    },
    // 表单重置
    reset() {
      this.form = {
        corporateName: undefined,
        placeOfOrigin: undefined,
        corporateAddress: undefined,
        antiCounterfeitingFrequency: undefined,
        content: undefined,
        remark: undefined,
        officialQrCodeUrl: undefined
      }
    },
    // 新建按钮
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加信息"
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 编辑按钮
    handleUpdate(row) {
      this.reset()
      infoConfigDetail(row.id).then(res => {
        this.open = true
        this.title = "编辑信息"
        this.form = res.data
      })
    },
    // 上传产品图片
    onUploadType(item){
      this.$modal.loading("正在上传")
      let formData = new FormData()
      let file = item.raw
      formData.append('file', file)
      uploadImg(formData).then(res => {
        this.$modal.closeLoading()
        this.$modal.msgSuccess("上传成功")
        this.form.officialQrCodeUrl = res.data.fileUrl
      })
    },
    // 提交按钮
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          infoConfigAdd(this.form).then(res => {
            if(res.code == 200){
              this.$modal.msgSuccess("操作成功")
              this.open = false
              this.getList()
            }
          })
        }
      })
    },
  }
}
</script>

<style scoped>
.avatar-uploader{
  display: flex;
  align-items: center;
  justify-content: center;
  color:#606266;
  width: 150px;
  height: 150px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader:hover {
  border-color: #5ED4D4;
}
.avatar-uploader:hover .avatar-uploader-icon{
  color: #5ED4D4;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  display: block;
}
</style>
